int BinaryTreeHeight(struct TreeNode* root)
{
    if(root == NULL)
    {
        return 0;
    }
    int leftmaxDepth = BinaryTreeHeight(root->left);
    int rightmaxDepth = BinaryTreeHeight(root->right);
    return leftmaxDepth > rightmaxDepth ? leftmaxDepth + 1 : rightmaxDepth + 1;
}

bool isBalanced(struct TreeNode* root)
{
    if(root == NULL)
    {
        return true;
    }
    int leftHeight = BinaryTreeHeight(root->left);
    int rightHeight = BinaryTreeHeight(root->right);
    int max = leftHeight;
    int min = rightHeight;
    if(leftHeight < rightHeight)
    {
        min = leftHeight;
        max = rightHeight;
    }
    if(max - min > 1)
    {
        return false;
    }
    return isBalanced(root->left) && isBalanced(root->right);
}